#!/usr/bin/perl
use File::stat;
use Time::localtime;
##########################################################
## Screen capture program
## Revision 0.9.5 - 4/6/2011
##
## Usage: ./screenCapt.pl host_file
#Hosts need to run scrot
#Server needs to run imageMagick
my @nodeList;    #list of nodes
my $script;      #script that will be run
my $nodeFile;    #file containing list of nodes
##sub to grab nodes specified
#sub nodeList
#{
#    #read in list of nodes to walk
#    print "-----------------------  Starting node listing  -----------------------\n";
#    open(NODES, $nodeFile);
#    while(<NODES>)
#    {
#        chomp $_;
#        push(@nodeList, $_);
#        #Add sanity checking here (next revision)
#        print "Node $_ added\n";
#    }
#    close(NODES);
#    print "-----------------------  Node listing Complete  -----------------------\n\n\n";
#}
#Sub to call the nodes and ask for a screenshot
sub getScreen
{
	print
"-----------------------  Starting screenshot capture -----------------------\n";
	my $node = $_[0];
	my @output;
	print "Walking $node\n";

#print "Will run: \"ssh $node \"export DISPLAY=:0\; scrot -d 0 /tmp/$node.png\"\"\n";
	`ssh $node "/home/user/callScreen.pl"`;

	#if node returns failure
	if ( ${^CHILD_ERROR_NATIVE} != 0 )
	{
		print "SSH returned failure code: ${^CHILD_ERROR_NATIVE}\n";

		#put black screen with error (assuming node down)
		`convert -size 1280x1024 xc:black /tmp/screencapt/$node.png`;
`convert $node.png -gravity center -pointsize 75 -fill red -annotate 0x0 \"SSH connect failed\" $node.png`;
	}

	#success!!!!
	else
	{
		print "SSH returned success, transfering image\n";
		print
"Will execute: scp -p user\@$node:/tmp/screenshot.png /tmp/screencapt/$node.png\n";
		`scp -p user\@$node:/tmp/screenshot.png /tmp/screencapt/$node.png`;
		`chown www-data /tmp/screencapt/$node.png`;

		#push(@output, `ssh $node "rm /tmp/$node.png"`);
		#not necessary - causes more activity when it would just be overwritten
	}
	print
"-----------------------  Screenshot capture complete  -----------------------\n\n";
}

#Add datestamp to file
sub dateStamp
{
	my $fileName = $_[0];
	print
"-----------------------       Timestapming  $fileName      -----------------------\n";
	my $date = ctime( stat("/tmp/screencapt/$fileName")->mtime );

#print "Date: " . `ls -la /tmp/screencapt/$fileName | awk '{print \$7\" \", \$6}'` . "\n";
`convert $fileName -gravity SouthEast -pointsize 50 -stroke '#000C' -strokewidth 1 -fill white -annotate +50+20 \"$date\" $fileName`;
`convert $fileName -gravity SouthWest -pointsize 50 -stroke '#000C' -strokewidth 1 -fill white -annotate +50+20 \"%f\" $fileName`;
	print
"-----------------------  Timestapming  $fileName completed -----------------------\n\n";
}

#sub to do the final combination
sub combineAndSize
{
	if ( ( `ls -la /tmp/screencapt/ | wc -l` % 4 ) != 0 )
	{
		my $originalNumber =
		  `ls -l /tmp/screencapt/ | grep -v '^total' |  wc -l`
		  ;    #drop the total line
		my $mod = $originalNumber % 4;
		for ( my $count = 0 ; $count < $mod ; $count++ )
		{
			print "adding blank images\n";
`convert -size 1280x1024 xc:black /tmp/screencapt/zblank-$count.png`;
`convert /tmp/screencapt/zblank-$count.png -gravity center -pointsize 75 -fill blue -annotate 0x0 \"This space left empty\" /tmp/screencapt/zblank-$count.png`;
		}
	}
	print
"-----------------------  Combining images now   -----------------------\n";

	#Montage the images (in a 4 by X id)
`montage  /tmp/screencapt/*.png -background none -bordercolor blue -borderwidth 4 -geometry 1920x1080 -tile 4x /tmp/screencapt/combined.png`;

	#convert the now giant image to 50% size
	print
"-----------------------  Downscaling images now   -----------------------\n";
`convert /tmp/screencapt/combined.png -resize 50%  /tmp/screencapt/combined_small.png`;
}
$nodeFile = $ARGV[0];

#nodeList(); #list the nodes
#read in list of nodes to walk
print
  "-----------------------  Starting node listing  -----------------------\n";
open( NODES, $nodeFile );
while (<NODES>)
{
	chomp $_;
	push( @nodeList, $_ );

	#Add sanity checking here (next revision)
	print "Node $_ added\n";
}
close(NODES);
print
"-----------------------  Node listing Complete  -----------------------\n\n\n"
  ;

#logic to add temporary directory if it does not exist already
#present only after a reboot
if ( -d '/tmp/screencapt' ) { }
else { `mkdir /tmp/screencapt` }
chdir "/tmp/screencapt";    #change the working direcotry to tmp
print "Cleaning /tmp/screencapt/\n";
`rm -f /tmp/screencapt/*`
  ;    #clean the temporary directory - keep the final image clean
foreach my $node (@nodeList)
{
	chomp($node);
	getScreen($node);
}
my @dirList = <*>;
foreach my $file (@dirList)
{
	chomp($file);
	dateStamp($file);
}
combineAndSize();
print "Completed run @ " . `date` . "\n";
`cp -f /tmp/screencapt/combined_small.png /var/www/screencap/combined_small.png`;
`chown www-data /var/www/screencap/combined_small.png`;
